using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;

namespace Webber
{
    public partial class Form2 : Form, IExportMonitor
    {
        private bool m_isDone;
        public volatile bool _shouldStop;
        private XmlNode m_notebook;
        private XmlNode[] m_sections;
        private string m_path;

        private Thread trd;

        public Form2(XmlNode notebook, XmlNode[] sections, string path)
        {
            InitializeComponent();
            m_isDone = false;
            _shouldStop = false;
            m_notebook = notebook;
            m_sections = sections;
            m_path = path;
        }

        ////////////////////////////////////
        // IExportMonitor Implementation
        ////////////////////////////////////
        delegate void SetRangeCallback(int min, int max);
        public void SetRange(int min, int max)
        {
            if (progressBar.InvokeRequired)
            {
                SetRangeCallback d = new SetRangeCallback(SetRange);
                this.Invoke(d, new object[] {min, max});
            }
            else
            {
                progressBar.Minimum = min;
                progressBar.Maximum = max;
            }
        }

        delegate void IncrementCallback(int i);
        public void Increment(int i)
        {
            if (progressBar.InvokeRequired)
            {
                IncrementCallback d = new IncrementCallback(Increment);
                this.Invoke(d, new object[] { i });
            }
            else
            {
                progressBar.Increment(i);
            }
        }

        delegate void AppendLogCallback(string msg);
        public void AppendLog(string msg)
        {
            if (txtLog.InvokeRequired)
            {
                AppendLogCallback d = new AppendLogCallback(AppendLog);
                this.Invoke(d, new object[] { msg });
            }
            else
            {
                txtLog.Text += msg + "\r\n";
                txtLog.SelectionStart = txtLog.Text.Length;
                txtLog.ScrollToCaret();
            }
        }

        delegate void CurrentStatusCallback(string msg);
        public void CurrentStatus(string msg)
        {
            if (lblCurrentStatus.InvokeRequired)
            {
                CurrentStatusCallback d = new CurrentStatusCallback(CurrentStatus);
                this.Invoke(d, new object[] { msg });
            }
            else
            {
                lblCurrentStatus.Text = msg;
            }
        }

        delegate void NoParamCallback();     
        public void Reset()
        {
            if (progressBar.InvokeRequired)
            {
                NoParamCallback d = new NoParamCallback(Reset);
                this.Invoke(d);
            }
            else
            {
                progressBar.Value = progressBar.Minimum;
            }
        }
        public void ClearLog()
        {
            if (txtLog.InvokeRequired)
            {
                NoParamCallback d = new NoParamCallback(ClearLog);
                this.Invoke(d);
            }
            else
            {
                txtLog.Text = "";
            }
        }

        delegate void SetDoneCallback(bool status);
        public void SetDone(bool status)
        {
            if (this.InvokeRequired)
            {
                SetDoneCallback d = new SetDoneCallback(SetDone);
                this.Invoke(d, new object[] { status });
            }
            else
            {
                m_isDone = true;
                btnPreview.Enabled = true;
                btnToggle.Text = "Close";
            }
        }

        public bool ShouldStop()
        {
            return _shouldStop;
        }

        private void btnToggle_Click(object sender, EventArgs e)
        {
            if (!m_isDone)
            {
                m_isDone = true;
                _shouldStop = true;
                btnToggle.Text = "&Close";
            }
            else
            {
                this.Close();
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            trd = new Thread(new ThreadStart(this.work));
            trd.IsBackground = true;
            trd.Start();

            this.BringToFront();
            this.Activate();
        }

        private void work()
        {
            ONWebExport onWebExp = new ONWebExport(m_notebook, m_sections, m_path, this);
            onWebExp.ExportNotebook();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            Uri target = new Uri(m_path);
            System.Diagnostics.Process.Start("IExplore.exe",target.AbsoluteUri); 
        }
    }
}